package per.hnvcam.searchext.text.token.impl;

import per.hnvcam.searchext.Constants;
import per.hnvcam.searchext.text.token.TokenHandler;
import per.hnvcam.searchext.text.token.Tokenizer;

/**
 * Created by IntelliJ IDEA.
 * User: violon
 * Date: 4/24/11
 * Time: 6:08 PM
 * To change this template use File | Settings | File Templates.
 */
public class DefaultTokenizerImpl implements Tokenizer {
    private String input;
    private int inputLength;
    private int currentCharPos;
    private char wordDelimeter;
    private String phraseDelimeters;
    private TokenHandler handler;

    public DefaultTokenizerImpl() {
        this.wordDelimeter = Constants.WORD_DELIMETER;
        setPhraseDelimeters(Constants.PHRASE_DELIMETERS);
    }

    public void lex(String input) {
        this.input = input;
        this.inputLength = input.length();
        this.currentCharPos = 0;
        getTokens();
    }

    private void getTokens() {
        if (handler == null) {
            return;
        }
        StringBuilder buffer = new StringBuilder();
        boolean sameAsPrev = false;
        while(this.currentCharPos < this.inputLength) {
            char currentChar = input.charAt(currentCharPos);
            currentCharPos++;
            if (currentChar == this.wordDelimeter) {
                if (buffer.length() > 0) {
                    handler.handleToken(buffer.toString(), sameAsPrev);
                    sameAsPrev = true;
                    buffer.setLength(0);
                } // else just ignore this wordDelimeter
            } else if (this.phraseDelimeters.indexOf(currentChar) >= 0) {
                if (buffer.length() > 0) {
                    handler.handleToken(buffer.toString(), sameAsPrev);
                }
                sameAsPrev = false;
                buffer.setLength(0);
            } else {
                buffer.append(currentChar);
            }

            // Last word
            if (currentCharPos == this.inputLength && buffer.length() > 0 ) {
                handler.handleToken(buffer.toString(), true);
            }
        }
    }

    public void setWordDelimeter(char wordDelimeter) {
        this.wordDelimeter = wordDelimeter;
    }

    public void setPhraseDelimeters(String phraseDelimeters) {
        this.phraseDelimeters = phraseDelimeters;
    }

    public void setHandler(TokenHandler handler) {
        this.handler = handler;
    }
}
